﻿package com.scn.base.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.scn.base.constant.Constants;
import com.scn.base.dao.impl.DAOManager;
import com.scn.base.domain.MenuVO;
import com.scn.base.page.TreeNode;
import com.scn.base.service.ISysMenuService;

@Service("sysMenuService")
public class SysMenuServiceImpl implements ISysMenuService {
	@Autowired
	private DAOManager daoManager;

	public List selectMenu() {
		// TODO Auto-generated method stub
		return daoManager.getSysMenuDao().selectMenu();
	}

	public List selectRolePowerList(int srid) {
		// TODO Auto-generated method stub
		return daoManager.getSysMenuDao().selectRolePowerList(srid);
	}

	public List selectRolePower(int srid) {
		// TODO Auto-generated method stub
		return daoManager.getSysMenuDao().selectRolePower(srid);
	}

	public TreeNode selectMenuList() {
		List<MenuVO> menuList = daoManager.getSysMenuDao().selectMenuAll();
		TreeNode root = new TreeNode();
		root.setId("1");
		root.setName("root");
		for (MenuVO menu : menuList) {
			TreeNode node = new TreeNode();
			node.setId(menu.getId());
			node.setName(menu.getName());
			node.setEname(menu.getEname());
			node.setLink(menu.getUrl());
			node.setImg(menu.getImg());
			node.setView(menu.getView());
			node.setOrder(menu.getOrderby());
			node.setParent(root.findChild(menu.getPid()));
			node.getParent().addChild(node);
		}
		return root;
	}

	public int checkMenuName(String smname) {
		if (smname == null || "".equals(smname.trim())) {
			return 2;
		}
		int count = daoManager.getSysMenuDao().checkMenuName(smname);
		if (count == 0) {
			return 0;
		} else {
			return 1;
		}
	}

	public void saveMenu(MenuVO menu) {
		String pid = menu.getPid().trim();
		Object o = daoManager.getSysMenuDao().selectMaxIdByPid(menu.getPid());
		String id = "";
		if (o == null) {
			id = pid + "01";
		} else {
			id = Integer.parseInt(o.toString()) + 1 + "";
		}
		menu.setId(id);
		menu.setImg(Constants.IMAGE_PREFIX + menu.getImg()
				+ Constants.IMAGE_PREFIX);
		daoManager.getSysMenuDao().insertMenu(menu);
	}

	public List<String> selectParentMenuForOption() {
		List<MenuVO> menuList = daoManager.getSysMenuDao().selectMenuAll();
		List<String> resultList = new ArrayList<String>();
		TreeNode root = new TreeNode();
		root.setId("1");
		root.setName("root");
		for (MenuVO menu : menuList) {
			TreeNode node = new TreeNode();
			node.setId(menu.getId());
			node.setName(menu.getName());
			node.setEname(menu.getEname());
			node.setLink(menu.getUrl());
			node.setImg(menu.getImg());
			node.setView(menu.getView());
			node.setOrder(menu.getOrderby());
			node.setParent(root.findChild(menu.getPid()));
			node.getParent().addChild(node);
		}
		Vector<TreeNode> first = root.getAllChild();
		for (TreeNode n : first) {
			resultList.add("<option value='" + n.getId() + "'>" + n.getName()
					+ "</option>");
			if (n.hasChild()) {// 第一级菜单
				Vector<TreeNode> second = n.getAllChild();
				for (TreeNode p : second) {// 第二级菜单
					resultList.add("<option value='" + p.getId() + "'>--"
							+ p.getName() + "</option>");
				}
			}
		}
		return resultList;
	}

}
